<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<div id="box"></div>
<button id="btn">抽奖</button>

<body>
  <script>
    let firstPrize = ["媳妇", "跑车", "别墅", "游艇", "现金10亿", "秘书10个"];
    let secondPrize = ["2A", "2C", "2D", "2E", "2F", "2T", "2k", "2X"];
    let thirdPrize = ["3A", "3C", "3D", "3E", "3F", "3T", "3k", "3X"];

    // function Draw(first = 2, second = 3, third = 5) {
    //   var result = [];//存放奖品的数组
    //   var random//每次的随机数
    //   // 一等奖
    //   for (let i = 0; i < first; i++) {
    //     random = Math.floor(Math.random() * firstPrize.length); //随机奖品下标
    //     result = result.concat(firstPrize[random]) //把奖品存放到数组
    //   }
    //   // 二等奖
    //   for (let i = 0; i < second; i++) {
    //     random = Math.floor(Math.random() * secondPrize.length); //随机奖品下标
    //     result = result.concat(secondPrize[random]) //把奖品存放到数组
    //   }
    //   // 三等奖
    //   for (let i = 0; i < third; i++) {
    //     random = Math.floor(Math.random() * thirdPrize.length); //随机奖品下标
    //     result = result.concat(thirdPrize[random]) //把奖品存放到数组
    //   }
    //   return result
    // }
    // var btn = document.getElementById("btn");
    // var box = document.getElementById("box")
    // btn.onclick = function () {
    //   console.log(Draw())
    //   box.innerHTML = Draw();
    // }
    //  使用generator函数 抽奖
    function* draw(first = 2, second = 3, third = 5) {
      var count = 0;//总抽奖次数

      while (1) {
        if (count < first) {
          // 一等奖
          random = Math.floor(Math.random() * firstPrize.length)
          yield firstPrize.splice(random, 1)
          count++;
        } else if (count < first + second) {
          // 二等奖
          random = Math.floor(Math.random() * secondPrize.length)
          yield secondPrize.splice(random, 1)
          count++
        } else if (count < first + second + third) {
          // 二等奖
          random = Math.floor(Math.random() * thirdPrize.length)
          yield thirdPrize.splice(random, 1)
          count++
        } else {
          return false
        }
      }
    }
    var btn = document.getElementById("btn");
    var box = document.getElementById("box")
    var d = draw();
    btn.onclick = function () {
      box.innerHTML = d.next().value;
    }




  </script>
</body>

</html>